package com.heima.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.storage.entity.Storage;
import com.heima.storage.mapper.StorageMapper;
import com.heima.storage.service.IStorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcm
 * @since 2021-09-22
 */
@Service
@Slf4j
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {

    @Override
    public void deduct(String code, Integer count) {
        log.info("开始扣减库存");
        try {
            LambdaUpdateWrapper<Storage> update = new LambdaUpdateWrapper<>();
            update.eq(Storage::getCommodityCode, code);
            update.setSql("count = count - " + count);
            this.update(update);
        } catch (Exception e) {
            throw new RuntimeException("扣减库存失败，可能是库存不足！", e);
        }
        log.info("扣减库存成功");
    }
}
